package com.touchyourheart.web.util;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@Component
public class FtlTemplateUtils {

  @Autowired
  private FreeMarkerConfigurer freeMarkerConfigurer;

  public String getEmailContent(String templatePath, Map<String, Object> contentValue) throws IOException,
      TemplateException {
    Template template = getTemplate(templatePath);
    String t = FreeMarkerTemplateUtils.processTemplateIntoString(template, contentValue);
    return t;
  }

  private Template getTemplate(String templateName) throws IOException {
    return freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
  }

}
